import { runApp, IAppConfig, logger, request } from 'ice';
import { message } from 'antd';
import Cookies from 'js-cookie';
import Config from '@/api/GlobalConfig';

const appConfig: IAppConfig = {
  app: {
    rootId: 'ice-container',
  },
  request: {
    baseURL: process.env.NODE_ENV === 'development' ? '/api' : '/api',
    interceptors: {
      request: {
        onConfig: (config) => {
          // 发送请求前：可以对 RequestConfig 做一些统一处理
          const token = localStorage.getItem('token') || '';
          if (token) {
            config.headers = { ...config.headers, Authorization: 'jwt ' + token };
          }
          return config;
        },
        onError: (error) => {
          return Promise.reject(error);
        },
      },
      response: {
        onConfig: (response) => {
          if (!response.data.success) {
            message.warning(response.data.message);
            return Promise.reject(response.data.message);
          }
          return { ...response, data: response.data.data };
        },
        onError: (error) => {
          if (error.response?.data.message) {
            logger.log(error.response);
            message.error(error.response.data.message);
          }
          if (error.response?.status === 403) {
            window.location.href =
              process.env.NODE_ENV === 'development'
                ? 'http://localhost:3333/#/login'
                : 'http://139.224.104.113/#/login';
          }
          // 请求出错：服务端返回错误状态码
          return Promise.reject(error);
        },
      },
    },
  },
};

runApp(appConfig);
